﻿using System;
using System.Windows;
using System.Windows.Media;
using SegmentDisplay.Common;

namespace SegmentDisplay.Custom.Time
{
    /// <summary>
    /// Control IndicatorDate
    /// </summary>
    public class IndicatorTime : SegmentBase
    {
        #region Style properties

        /// <summary>
        /// Gets or sets digit style.
        /// </summary>
        public Style DigitStyle
        {
            get { return (Style)GetValue(DigitStyleProperty); }
            set { SetValue(DigitStyleProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the digit style.
        /// </summary>
        public static DependencyProperty DigitStyleProperty =
            DependencyProperty.Register("DigitStyle", typeof(Style), typeof(IndicatorTime));

        /// <summary>
        /// Gets or sets separator style.
        /// </summary>
        public Style SeparatorStyle
        {
            get { return (Style)GetValue(SeparatorStyleProperty); }
            set { SetValue(SeparatorStyleProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the separator style.
        /// </summary>
        public static DependencyProperty SeparatorStyleProperty =
            DependencyProperty.Register("SeparatorStyle", typeof(Style), typeof(IndicatorTime));

        #endregion

        #region Input values

        /// <summary>
        /// Input value 
        /// </summary>
        public DateTime Value
        {
            get { return (DateTime)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the Value.
        /// </summary>
        public static DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(DateTime), typeof(IndicatorTime), new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(ValueChanged)));

        /// <summary>
        /// Function on change Value property 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            IndicatorTime sh = (IndicatorTime)d;
            DateTime _currentValue = DateTime.Now;
            if (e.NewValue != null)
            {
                if (e.NewValue is DateTime)
                {
                    DateTime digit = (DateTime)e.NewValue;
                    _currentValue = digit;
                }
                sh.UpdateSegments(sh.IsSwitchOn, _currentValue);
            }
        }

        /// <summary>
        /// Changing on change a value 
        /// </summary>
        /// <param name="isSwitchOn"></param>
        /// <param name="newValue"></param>
        protected void UpdateSegments(bool isSwitchOn, DateTime newValue)
        {
            if (isSwitchOn)
                SetDigit(newValue);
            else
                SetDigit();
        }

        /// <summary>
        /// Default update with current values
        /// </summary>
        protected void UpdateSegments()
        {
            UpdateSegments(IsSwitchOn, Value);
        }

        /// <summary>
        /// public function for force change the value
        /// </summary>
        /// <param name="digit"></param>
        public void SetDigit(DateTime digit)
        {
            try
            {
                string hours = digit.Hour.ToString("00");
                string minutes = digit.Minute.ToString("00");
                string seconds = digit.Second.ToString("00");
                int h1, h2, m1, m2, s1, s2;

                int.TryParse(hours.Substring(0, 1), out h1); H1 = h1.ToString();
                int.TryParse(hours.Substring(1, 1), out h2); H2 = h2.ToString();
                int.TryParse(minutes.Substring(0, 1), out m1); M1 = m1.ToString();
                int.TryParse(minutes.Substring(1, 1), out m2); M2 = m2.ToString();
                int.TryParse(seconds.Substring(0, 1), out s1); S1 = s1.ToString();
                int.TryParse(seconds.Substring(1, 1), out s2); S2 = s2.ToString();
                Delimiter = !Delimiter;
            }
            catch (Exception )
            {
                SetDigit();
            }
        }
        public void SetDigit()
        {
            H1 = H2 = M1 = M2 = S1 = S2 = "Empty";
            Delimiter = false;
        }

        #endregion

        #region Management of serment

        /// <summary>
        /// Override a behavior on changing the IsSwitchOn value
        /// </summary>
        /// <param name="newValue"></param>
        public override void OnIsSwitchOnChanged(bool newValue)
        {
            base.OnIsSwitchOnChanged(newValue);
            UpdateSegments(newValue, Value);
        }

        #endregion

        #region Value

        public string H1
        {
            get { return (string)GetValue(H1Property); }
            set { SetValue(H1Property, value); }
        }
        public static DependencyProperty H1Property = DependencyProperty.Register("H1", typeof(string), typeof(IndicatorTime), new PropertyMetadata("Empty"));

        public string H2
        {
            get { return (string)GetValue(H2Property); }
            set { SetValue(H2Property, value); }
        }
        public static DependencyProperty H2Property = DependencyProperty.Register("H2", typeof(string), typeof(IndicatorTime), new PropertyMetadata("Empty"));

        public string M1
        {
            get { return (string)GetValue(M1Property); }
            set { SetValue(M1Property, value); }
        }
        public static DependencyProperty M1Property = DependencyProperty.Register("M1", typeof(string), typeof(IndicatorTime), new PropertyMetadata("Empty"));

        public string M2
        {
            get { return (string)GetValue(M2Property); }
            set { SetValue(M2Property, value); }
        }
        public static DependencyProperty M2Property = DependencyProperty.Register("M2", typeof(string), typeof(IndicatorTime), new PropertyMetadata("Empty"));

        public string S1
        {
            get { return (string)GetValue(S1Property); }
            set { SetValue(S1Property, value); }
        }
        public static DependencyProperty S1Property = DependencyProperty.Register("S1", typeof(string), typeof(IndicatorTime), new PropertyMetadata("Empty"));

        public string S2
        {
            get { return (string)GetValue(S2Property); }
            set { SetValue(S2Property, value); }
        }
        public static DependencyProperty S2Property = DependencyProperty.Register("S2", typeof(string), typeof(IndicatorTime), new PropertyMetadata("Empty"));

        public bool Delimiter
        {
            get { return (bool)GetValue(DelimiterProperty); }
            set { SetValue(DelimiterProperty, value); }
        }
        public static DependencyProperty DelimiterProperty = DependencyProperty.Register("Delimiter", typeof(bool), typeof(IndicatorTime), new PropertyMetadata(false));

        
        #endregion

        #region Constructor 

        /// <summary>
        /// Static constructor
        /// </summary>
        static IndicatorTime()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(IndicatorTime), new FrameworkPropertyMetadata(typeof(IndicatorTime)));
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public IndicatorTime()
        {
            DefaultStyleKey = typeof(IndicatorTime);
        }

        #endregion
    }
}
